#include <vector>
#include <iostream>

using namespace std;

struct ListNode
{
    int val;
    ListNode * next;
    ListNode() : val(0), next(nullptr)
    {}
    ListNode(int val) : val(val), next(nullptr)
    {}
    ListNode(int val, ListNode * next) : val(val), next(next)
    {}
};


class Solution 
{
public:
    ListNode* reverseList(ListNode* head) 
    {
        if(head == nullptr || head->next == nullptr)
        {
            return head;
        }
        ListNode * pre = nullptr;
        ListNode * cur = head;
        ListNode * nex = head->next;
        while(1)
        {
            cur->next = pre;
            pre = cur;
            cur = nex;
            if(cur == nullptr)
            {
                break;
            }
            nex = nex->next;
        }
        return pre;
    }
};

// 辅助函数：创建链表
ListNode* createList(vector<int>& vals) 
{
    if(vals.empty()) return nullptr;
    
    ListNode* head = new ListNode(vals[0]);
    ListNode* cur = head;
    for(int i = 1; i < vals.size(); i++) 
    {
        cur->next = new ListNode(vals[i]);
        cur = cur->next;
    }
    return head;
}

// 辅助函数：打印链表
void printList(ListNode* head) 
{
    cout << "[";
    while(head) 
    {
        cout << head->val;
        if(head->next) cout << ",";
        head = head->next;
    }
    cout << "]";
}

// 辅助函数：将链表转换为vector（用于验证）
vector<int> listToVector(ListNode* head) 
{
    vector<int> result;
    while(head) 
    {
        result.push_back(head->val);
        head = head->next;
    }
    return result;
}

int main() 
{
    Solution solution;
    
    // 测试案例1: [1,2,3,4,5]
    cout << "测试案例1:" << endl;
    vector<int> vals1 = {1, 2, 3, 4, 5};
    ListNode* head1 = createList(vals1);
    cout << "输入: ";
    printList(head1);
    cout << endl;
    ListNode* result1 = solution.reverseList(head1);
    cout << "输出: ";
    printList(result1);
    cout << endl;
    cout << "期望: [5,4,3,2,1]" << endl << endl;
    
    // 测试案例2: [1,2]
    cout << "测试案例2:" << endl;
    vector<int> vals2 = {1, 2};
    ListNode* head2 = createList(vals2);
    cout << "输入: ";
    printList(head2);
    cout << endl;
    ListNode* result2 = solution.reverseList(head2);
    cout << "输出: ";
    printList(result2);
    cout << endl;
    cout << "期望: [2,1]" << endl << endl;
    
    // 测试案例3: 空链表
    cout << "测试案例3 (空链表):" << endl;
    ListNode* head3 = nullptr;
    cout << "输入: []" << endl;
    ListNode* result3 = solution.reverseList(head3);
    cout << "输出: ";
    printList(result3);
    cout << endl;
    cout << "期望: []" << endl << endl;
    
    // 测试案例4: 单节点链表
    cout << "测试案例4 (单节点链表):" << endl;
    vector<int> vals4 = {1};
    ListNode* head4 = createList(vals4);
    cout << "输入: ";
    printList(head4);
    cout << endl;
    ListNode* result4 = solution.reverseList(head4);
    cout << "输出: ";
    printList(result4);
    cout << endl;
    cout << "期望: [1]" << endl << endl;
    
    // 测试案例5: 长链表
    cout << "测试案例5 (长链表):" << endl;
    vector<int> vals5 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    ListNode* head5 = createList(vals5);
    cout << "输入: ";
    printList(head5);
    cout << endl;
    ListNode* result5 = solution.reverseList(head5);
    cout << "输出: ";
    printList(result5);
    cout << endl;
    cout << "期望: [10,9,8,7,6,5,4,3,2,1]" << endl << endl;
    
    // 测试案例6: 包含负数
    cout << "测试案例6 (包含负数):" << endl;
    vector<int> vals6 = {-1, 0, 1, 2};
    ListNode* head6 = createList(vals6);
    cout << "输入: ";
    printList(head6);
    cout << endl;
    ListNode* result6 = solution.reverseList(head6);
    cout << "输出: ";
    printList(result6);
    cout << endl;
    cout << "期望: [2,1,0,-1]" << endl << endl;
    
    // 测试案例7: 相同元素
    cout << "测试案例7 (相同元素):" << endl;
    vector<int> vals7 = {1, 1, 1, 1};
    ListNode* head7 = createList(vals7);
    cout << "输入: ";
    printList(head7);
    cout << endl;
    ListNode* result7 = solution.reverseList(head7);
    cout << "输出: ";
    printList(result7);
    cout << endl;
    cout << "期望: [1,1,1,1]" << endl << endl;
    
    // 测试案例8: 两个节点
    cout << "测试案例8 (两个节点):" << endl;
    vector<int> vals8 = {5, 3};
    ListNode* head8 = createList(vals8);
    cout << "输入: ";
    printList(head8);
    cout << endl;
    ListNode* result8 = solution.reverseList(head8);
    cout << "输出: ";
    printList(result8);
    cout << endl;
    cout << "期望: [3,5]" << endl << endl;
    
    // 测试案例9: 大数值
    cout << "测试案例9 (大数值):" << endl;
    vector<int> vals9 = {100, 200, 300};
    ListNode* head9 = createList(vals9);
    cout << "输入: ";
    printList(head9);
    cout << endl;
    ListNode* result9 = solution.reverseList(head9);
    cout << "输出: ";
    printList(result9);
    cout << endl;
    cout << "期望: [300,200,100]" << endl << endl;
    
    // 测试案例10: 三个节点
    cout << "测试案例10 (三个节点):" << endl;
    vector<int> vals10 = {1, 2, 3};
    ListNode* head10 = createList(vals10);
    cout << "输入: ";
    printList(head10);
    cout << endl;
    ListNode* result10 = solution.reverseList(head10);
    cout << "输出: ";
    printList(result10);
    cout << endl;
    cout << "期望: [3,2,1]" << endl << endl;
    
    return 0;
}